package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.PermissionService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Permission;
import io.jsonwebtoken.lang.Collections;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/admin/acl/permission")
@CrossOrigin
@Api(tags = "菜单管理")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
//    查询所有菜单
    @GetMapping
    @ApiOperation("获取权限(菜单/功能)列表")
    public Result getPermissionList(){
        List<Permission> permissionList = permissionService.queryAllPermission();
        return Result.ok(permissionList);
    }
//    添加菜单
    @PostMapping("/save")
    @ApiOperation("保存一个权限项")
    public Result addPermission(@RequestBody Permission permission){
        if (permissionService.save(permission)) {
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }
//    修改菜单
    @PutMapping("/update")
    @ApiOperation("更新一个权限值")
    public Result updatePermission(@RequestBody Permission permission){
        if (permissionService.updateById(permission)) {
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }
//    删除菜单 递归删除
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除一个权限数值")
    public Result removePermission(@PathVariable Long id){
        Boolean sureDelete = permissionService.removeChildById(id);
        return Result.ok(sureDelete);
    }
}
